<template>
	<view class="app">
		<view class="panel">
			<view class="flex-col setpidpage">
				<view class="flex justify-between inputitem">
					<view><input v-model="searchkey" placeholder="请输入用户ID、手机或昵称" /></view>
				</view>
				<view class="userinfo" v-if="viewuser">
					<view>用户昵称:<text>{{user.nickname}}</text></view>
					<view>消费次数:<text>{{user.pay_num}}次</text></view>
					<view>加钟次数:<text>{{user.clock_num}}次</text></view>
				</view>
				<view class="flex justify-center items-center btn-save">
					<button @tap.stop="search">查询</button>
				</view>
			</view>
		</view>
		<view class="panel">
			<view class="flex-col setpidpage">
				<view class="flex-col">
					<checkbox-group @change="changeCheckBox">
						<label class="flex justify-start items-center" v-for="(item,index) in coupons">
							<checkbox class="mycheck" :value="item.id.toString()">{{item.money}} - {{item.name}}</checkbox>
						</label>
					</checkbox-group>
				</view>
				<view class="flex justify-center items-center btn-save">
					<button @tap.stop="save">立即赠送</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var that,App=getApp(),Comm=getApp().comm;
	export default {
		data() {
			return {
				searchkey:'',
				user:{},
				viewuser:false,
				coupons:[],
				cid:[],
			}
		},
		onLoad() {
			that=this;
			this.comm.http('/api/service/coupons',null,'GET').then(res=>{
				that.coupons=res.data;
			});
		},
		methods: {
			search(){
				this.comm.http('/api/service/userinfo',{search_key:this.searchkey}).then(res=>{
					if(res.code!=1){
						that.viewuser=false;
						uni.showToast({icon:'none',title:res.msg});
					}else{
						that.user=res.data;
						that.viewuser=true;
					}
				});
			},
			changeCheckBox(e){
				this.cid=e.detail.value;
			},
			save(){
				if(!/\d+/.test(this.user.id)){
					uni.showToast({
						title:'请先查询用户信息',
						icon:'none'
					});
					return
				}
				if(this.cid.length<1){
					uni.showToast({
						title:'请选择优惠券类型',
						icon:'none'
					});
					return
				}
				
				uni.showModal({
					content:'确认赠送优惠券?',
					complete(e) {
						if(e.confirm){
							that.comm.http('/api/service/exchange',{uid:that.user.id,cid:(that.cid).join(',')}).then(res=>{
								uni.showToast({icon:'none',title:res.msg});
							});
						}
					}
				})
				
			}
		}
	}
</script>

<style lang="scss">
	.setpidpage{
		.inputitem{
			align-items: center;

			view:nth-child(1){
				flex: 1;
				background-color: #f0f0f0; border-radius: 10px; padding: 0 8px;
				input{
					height:40px; width: 100%;  font-size: 13px; 
				}
			}

		}
		.tips{
			font-size: 12px; color: #999; padding: 8px 0; display: block;
		}
	}
	.mycheck{
		margin-top: 8px;
	}
	.userinfo{
		margin: 8px 0;
	}
	.userinfo>view>text{
		margin-left: 5px; color: #666; font-weight: bold;
	}
</style>
